﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NeonMika.EightTracks
{
    public class User
    {

        string slug;

        public string Slug
        {
            get { return slug; }
            set { slug = value; }
        }
        string location;

        public string Location
        {
            get { return location; }
            set { location = value; }
        }
        string popup_prefs;

        public string PopupPrefs
        {
            get { return popup_prefs; }
            set { popup_prefs = value; }
        }
        string next_mix_prefs;

        public string NextMixPrefs
        {
            get { return next_mix_prefs; }
            set { next_mix_prefs = value; }
        }
        Dictionary<CoverFormat, string> avatar_urls;

        internal Dictionary<CoverFormat, string> AvatarUrls
        {
            get { return avatar_urls; }
            set { avatar_urls = value; }
        }
        string bio_html;

        public string BioHtml
        {
            get { return bio_html; }
            set { bio_html = value; }
        }
        string subscribed;

        public string Subscribed
        {
            get { return subscribed; }
            set { subscribed = value; }
        }
        string login;

        public string Login
        {
            get { return login; }
            set { login = value; }
        }
        int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        bool followed_by_current_user;

        public bool FollowedByCurrentUser
        {
            get { return followed_by_current_user; }
            set { followed_by_current_user = value; }
        }

        public string BiggestSqAvatar
        {

            get { return AvatarUrls.Keys.Contains(CoverFormat.sq500) ? AvatarUrls[CoverFormat.sq500] : AvatarUrls.Keys.Contains(CoverFormat.sq250) ? AvatarUrls[CoverFormat.sq250] : AvatarUrls.Keys.Contains(CoverFormat.sq133) ? AvatarUrls[CoverFormat.sq133] : AvatarUrls.Keys.Contains(CoverFormat.sq100) ? AvatarUrls[CoverFormat.sq100] : AvatarUrls.Keys.Contains(CoverFormat.sq72) ? AvatarUrls[CoverFormat.sq72] : AvatarUrls.Keys.Contains(CoverFormat.sq56) ? AvatarUrls[CoverFormat.sq56] : ""; }
        }

        public string BiggestMaxAvatar
        {
            get { return AvatarUrls.Keys.Contains(CoverFormat.max1024) ? AvatarUrls[CoverFormat.max1024] : AvatarUrls.Keys.Contains(CoverFormat.max250w) ? AvatarUrls[CoverFormat.max250w] : AvatarUrls.Keys.Contains(CoverFormat.max200) ? AvatarUrls[CoverFormat.max200] : AvatarUrls.Keys.Contains(CoverFormat.max133w) ? AvatarUrls[CoverFormat.max133w] : ""; }
        }

        public string SmallestSqAvatar
        {
            get { return AvatarUrls.Keys.Contains(CoverFormat.sq56) ? AvatarUrls[CoverFormat.sq56] : AvatarUrls.Keys.Contains(CoverFormat.sq72) ? AvatarUrls[CoverFormat.sq72] : AvatarUrls.Keys.Contains(CoverFormat.sq100) ? AvatarUrls[CoverFormat.sq100] :AvatarUrls.Keys.Contains(CoverFormat.sq133) ? AvatarUrls[CoverFormat.sq133] : AvatarUrls.Keys.Contains(CoverFormat.sq250) ? AvatarUrls[CoverFormat.sq250] : AvatarUrls.Keys.Contains(CoverFormat.sq500) ? AvatarUrls[CoverFormat.sq500] : "" ; }
        }

        public string SmallestMaxAvatar
        {
            get { return AvatarUrls.Keys.Contains(CoverFormat.max133w) ? AvatarUrls[CoverFormat.max133w] : AvatarUrls.Keys.Contains(CoverFormat.max200) ? AvatarUrls[CoverFormat.max200] :  AvatarUrls.Keys.Contains(CoverFormat.max250w) ? AvatarUrls[CoverFormat.max250w] : AvatarUrls.Keys.Contains(CoverFormat.max1024) ? AvatarUrls[CoverFormat.max1024] : ""; }
        }

        public User(XElement xml)
        {
            string element = "slug";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Slug = xml.Element(element).Value.ToString( );

            element = "location";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Location = xml.Element(element).Value.ToString( );

            element = "popup-prefs";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                PopupPrefs = xml.Element(element).Value.ToString( );

            element = "next-mix-prefs";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                NextMixPrefs = xml.Element(element).Value.ToString( );

            element = "bio-html";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                BioHtml = xml.Element(element).Value.ToString( );

            element = "subscribed";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Subscribed = xml.Element(element).Value.ToString( );

            element = "login";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Login = xml.Element(element).Value.ToString( );

            AvatarUrls = new Dictionary<CoverFormat, string>( );
            foreach ( XElement x in from cover in xml.Element("avatar-urls").Descendants( ) select cover )
                switch ( x.Name.LocalName )
                {
                    case "sq56":
                        AvatarUrls.Add(CoverFormat.sq56, x.Value.ToString( ));
                        break;
                    case "sq133":
                        AvatarUrls.Add(CoverFormat.sq133, x.Value.ToString( ));
                        break;
                    case "sq100":
                        AvatarUrls.Add(CoverFormat.sq100, x.Value.ToString( ));
                        break;
                    case "original":
                        AvatarUrls.Add(CoverFormat.original, x.Value.ToString( ));
                        break;
                    case "max200":
                        AvatarUrls.Add(CoverFormat.max200, x.Value.ToString( ));
                        break;
                    case "max133w":
                        AvatarUrls.Add(CoverFormat.max133w, x.Value.ToString( ));
                        break;
                }

            element = "id";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Id = Convert.ToInt32(xml.Element(element).Value);

            element = "followed-by-current-user";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                FollowedByCurrentUser = xml.Element(element).Value.ToString( ) == "true" ? true : false;
        }
    }
}
